package com.shinbada.modules.sys.entity;

import com.shinbada.common.utils.StringUtils;
import com.shinbada.core.persistence.DataEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

import java.util.Date;
import java.util.Map;

/**
 * 日志Entity
 *
 * @author initcap
 * @version 2017-8-19
 */
@Data
@ApiModel("系统日志")
public class Log extends DataEntity<Log> {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty("日志类型（1：接入日志；2：错误日志）")
    private String type;
    @ApiModelProperty("日志标题")
    private String title;
    @ApiModelProperty("操作用户的IP地址")
    private String remoteAddr;
    @ApiModelProperty("操作的URI")
    private String requestUri;
    @ApiModelProperty("操作的方式")
    private String method;
    @ApiModelProperty("操作提交的数据")
    private String params;
    @ApiModelProperty("操作用户代理信息")
    private String userAgent;
    @ApiModelProperty("异常信息")
    private String exception;

    @ApiModelProperty("开始日期")
    private Date beginDate;
    @ApiModelProperty("结束日期")
    private Date endDate;


    public Log() {
        super();
    }

    public Log(String id) {
        super(id);
    }

    /**
     * 设置请求参数
     *
     * @param paramMap
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    public void setParams(Map paramMap) {
        if (paramMap == null) {
            return;
        }
        StringBuilder params = new StringBuilder();
        for (Map.Entry<String, String[]> param : ((Map<String, String[]>) paramMap).entrySet()) {
            params.append(("".equals(params.toString()) ? "" : "&") + param.getKey() + "=");
            String paramValue = (param.getValue() != null && param.getValue().length > 0 ? param.getValue()[0] : "");
            params.append(StringUtils.abbr(StringUtils.endsWithIgnoreCase(param.getKey(), "password") ? "" : paramValue, 100));
        }
        this.params = params.toString();
    }

    @Override
    public String toString() {
        return ReflectionToStringBuilder.toString(this);
    }
}
